<?php
/**
 * Class Agent  TODO
 * Desc  代理Controller
 * Create on 2021/8/26 14:12
 * Create by wangyafang
 */

namespace app\api\controller\xyb;


use addons\xyb\XypHelper;
use app\admin\model\AuthGroup;
use app\admin\model\AuthGroupAccess;
use app\admin\model\xyb\system\Config;
use app\api\model\xyb\Order;
use app\api\model\xyb\OrderComplaint;
use app\api\model\xyb\Profit;
use app\api\model\xyb\UsersAppeal;
use app\api\model\xyb\Wallets;
use app\api\service\OrdersService;
use app\api\service\ProfitOrderService;
use app\api\service\UsersService;
use app\common\model\xyb\AdminBelong;
use app\common\model\xyb\UserBelong;
use app\api\model\xyb\OrderReceive;
use app\api\model\xyb\UserReceiveApply;
use app\api\model\xyb\UserReceive;
use app\api\model\xyb\UserPlat;
use app\api\model\xyb\UserWechat;
use think\Db;

class Account  extends Base
{
    protected $model = '';
    protected $noNeedLogin = [''];

    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

    public function _initialize(){
        parent::_initialize();
        $this->model = new \app\api\model\xyb\Agent();
    }

    /*个人中心*/
    public function index()
    {
        $users_belong_group = [1=>['平台用户'], 2=>['接单员'], 3=>['校园代理'], 4=>['校园代理', '接单员']];
        $users = $this->getAuthUsers();
        if(!$users_agent = UsersService::getCommunityWhereUsersLocated($users->id)){
            $this->success('获取成功', [
                'id'                => $users->id,
                'nickname'          => $users->nickname,
                'avatar'            => $users->avatar,
                'openid'            => $users->miniapp_openid,
                'unionid'           => $users->unionid,
                'is_agent' => false,
                'is_rider' => false,
            ]);
        }
        $identity_tags = $users_belong_group[$users_agent->users_identity];
        $evaluate = 5;
        $presence = false;
        if($users_agent){
            if(in_array($users_agent->users_identity, [2, 4])){
                if($users_agent->rider_info){
                    $evaluate = (float)$users_agent->rider_info->evaluate;//评分
                    $presence = ((int)$users_agent->rider_info->presence) === 1;
                }
            }
        }
        $systemConfigModel = new Config();
        $min_withdrawal_amount = (float)$systemConfigModel::getAgentConfig($users_agent->agent_id,'min_withdrawal_amount');
        $service_charge = (float)$systemConfigModel::getAgentConfig($users_agent->agent_id,'service_charge');
        $this->success('获取成功', [
            'id'                    => $users->id,
            'nickname'              => $users->nickname,
            'avatar'                => $users->avatar,
            'openid'                => $users->miniapp_openid,
            'unionid'               => $users->unionid,
            'identity_tags'         => $identity_tags,
            'evaluate'              => $evaluate,
            'min_withdrawal_amount' => $min_withdrawal_amount,
            'service_charge'       => $service_charge,
            'mobile'                => $users->mobile ? $users->mobile:(isset($users_agent->rider_info->mobile)?$users_agent->rider_info->mobile:''),
            'presence'              => $presence,//在线状态
            'user_type'             => $users_agent->users_identity, //1普通用户;2跑腿员;3代理商
            'identity'              => $users_agent->xyb_group_id?$users_agent->xyb_group_id:'', //1普通用户;2跑腿员;3代理商
            'is_agent'              => $users_agent->is_agent,
            'is_rider'              => $users_agent->is_rider,
            'rider_apply_status'    => isset($users_agent->rider_info->status)?intval($users_agent->rider_info->status):0,//0未申请，1申请中，2已通过，3已拒绝
            'rider_stated'          => isset($users_agent->rider_info->stated)?intval($users_agent->rider_info->stated):0,//是否封禁
            'rider_blacklist_reason' => isset($users_agent->rider_info->blacklist_reason)?($users_agent->rider_info->blacklist_reason):'',
            'rider_blacklist_picture' => isset($users_agent->rider_info->blacklist_picture)?XypHelper::stringToArray($users_agent->rider_info->blacklist_picture):[],//是否封禁
            'income_details'=> UsersService::alculateUserRevenue($users_agent->users_identity , $users->id, $users_agent->agent_id)
        ]);
    }

    /*跑腿员统计*/
    public function riderstatistics(){
        $users = $this->getAuthUsers();
        $profitModel = new Profit();
        $orderModel = new Order();
        $where['receive_id'] = array('eq', $users->id);
        $response = [
            'in_progress_order_count'       => $orderModel->where($where)->where('status','in',[2,8])->count(),
            'complete_order_count'          => $orderModel->where($where)->where('status','in',[3,7])->count(),
            'week'  => [
                'income'=>$profitModel->where($where)->whereTime('createtime', 'week')->sum('receive_profit'),
                'order_count'=>$orderModel->where($where)->where('status','in',[2,3,7,8])->whereTime('createtime', 'week')->count(),
            ],
            'month'  => [
                'income'=>$profitModel->where($where)->whereTime('createtime', 'month')->sum('receive_profit'),
                'order_count'=>$orderModel->where($where)->where('status','in',[2,3,7,8])->whereTime('createtime', 'month')->count(),
            ],
            'year'  => [
                'income'=>$profitModel->where($where)->whereTime('createtime', 'year')->sum('receive_profit'),
                'order_count'=>$orderModel->where($where)->where('status','in',[2,3,7,8])->whereTime('createtime', 'year')->count(),
            ],
        ];
        $this->success('获取成功', $response);
    }

    /*用户资金明细*/
    public function users_profit(){
        if(!$this->request->isPost()) {
            $this->error('请求类型错误');
        }
        $users = $this->getAuthUsers();
//        if(!$agent_id = UsersService::getProxyBoundToUserId($users->id)){
//            $this->error('您还未绑定代理');
//        }
        $users_agent = UsersService::getCommunityWhereUsersLocated($users->id);

        $profitModel = new Profit();
        $data = ['total' => 0, 'data' => 0];
        if($users_agent->users_identity ==2){//跑腿员
            $where['receive_id'] = array('eq',$users->id);
            $where['app_plat'] = array('eq','MP-WEIXIN');
            $data = $profitModel->getList($profitModel,$where,'desc,createtime,receive_profit','id desc');
            $data['show_column'] = ['desc','receive_profit','createtime'];
            $this->success('请求成功',$data);
        }
        elseif ($users_agent->users_identity ==3) //代理
        {
            $where['agent_id'] = array('eq',$users_agent->agent_id);
            $where['app_plat'] = array('eq','MP-WEIXIN');
            $data = $profitModel->getList($profitModel,$where,'desc,createtime,agent_profit','id desc');
            $data['show_column'] = ['desc','agent_profit','createtime'];

        } elseif ($users_agent->users_identity == 4) //跑腿员加代理
        {
            $where['agent_id'] = array('eq',$users_agent->agent_id);
            $where['app_plat'] = array('eq','MP-WEIXIN');
            $data = $profitModel->getUsersList($users->id, $users_agent->agent_id,'desc,createtime,agent_profit,receive_profit','id desc');
            $data['show_column'] = ['desc','agent_profit','receive_profit','createtime'];
        }
        $this->success('请求成功', $data);
    }

    public function presence(){
        $users = $this->getAuthUsers();
        $agent_info = UsersService::getCommunityWhereUsersLocated($users->id);
        $res = UsersService::setRiderOrderReceivingStatus($agent_info->agent_id, $users->id);
        $this->success('操作成功', ['presence'=>$res]);
    }

    public function rider_idle(){
        $users = $this->getAuthUsers();
        $agent_info = UsersService::getCommunityWhereUsersLocated($users->id);
        $data = UsersService::getRiderIdleUsersLists($agent_info->agent_id);
        $this->success('操作成功', $data);
    }

    public function rider_appeal(){
        $users = $this->getAuthUsers();
        if(!$agent_info = UsersService::getCommunityWhereUsersLocated($users->id)){
            $this->error('您还未绑定代理');
        }
        $picture = $this->request->param('picture', '');
        $content = $this->request->param('content', '');

        $res = UsersAppeal::submitAppealReply($agent_info->agent_id, $users->id, $content, $picture);
        $this->success('处理成功');
    }

    public function rider_appeal_details(){
        $users = $this->getAuthUsers();
        if(!$agent_info = UsersService::getCommunityWhereUsersLocated($users->id)){
            $this->error('您还未绑定代理');
        }
        if(!$res = UsersAppeal::getAppealReply($agent_info->agent_id, $users->id)){
            $this->error('暂无申诉');
        }
        $res->picture = XypHelper::stringToArray($res->picture);
        $res->reply_picture = XypHelper::stringToArray($res->reply_picture);
        $this->success('获取成功', $res);
    }















    public function agent_profit(){
        if(!$this->request->isPost()) {
            $this->error('请求类型错误');
        }
        $users = $this->getAuthUsers();
        if(!$agent_id = UsersService::getProxyBoundToUserId($users->id)){
            $this->error('您还未绑定代理');
        }
        $profitModel = new Profit();
        $where['agent_id'] = array('eq',$agent_id);
        $where['app_plat'] = array('eq','MP-WEIXIN');
        $data = $profitModel->getList($profitModel,$where,'desc,createtime,agent_profit as receive_profit','id desc');
        $this->success('请求成功',$data);
    }



















    public function rider_profit(){
        if(!$this->request->isPost()) {
            $this->error('请求类型错误');
        }
        $users = $this->getAuthUsers();
        $profitModel = new Profit();
        $user_id = $this->auth->id;
        $where['receive_id'] = array('eq',$users->id);
        $where['app_plat'] = array('eq','MP-WEIXIN');
        $data = $profitModel->getList($profitModel,$where,'desc,createtime,receive_profit','id desc');
        $this->success('请求成功',$data);
    }

    public function agent_profitsss(){
        if(!$this->request->isPost()) {
            $this->error('请求类型错误');
        }
        $users = $this->getAuthUsers();
        if(!$agent_id = UsersService::getProxyBoundToUserId($users->id)){
            $this->error('您还未绑定代理');
        }
        $profitModel = new Profit();
        $where['agent_id'] = array('eq',$agent_id);
        $where['app_plat'] = array('eq','MP-WEIXIN');
        $data = $profitModel->getList($profitModel,$where,'desc,createtime,agent_profit as receive_profit','id desc');
        $this->success('请求成功',$data);
    }
































    public static  function fadfadf($identity, $user_id, $agent_id){
        $response = [
            'total_income'  => 0,
            'today_income'  => 0,
            'can_withdrawal_amount'=>0,
        ];
        if($identity == 1){
            return $response;
        }
        $walletsModel = new Wallets();
        $rider = [];
        if($identity == 2 || $identity == 4){
            $walletsInfo = $walletsModel->where(['user_id'=>$user_id, 'type'=>'1'])->where('status', 'normal')->find();
            $rider = ProfitOrderService::calculateRiderIncome($user_id);
            $response['total_income'] = (float)$rider['total_income'];
            $response['today_income'] = (float)$rider['today_income'];
            $response['yesterday_income'] = (float)$rider['yesterday_income'];
            $response['seven_income'] = (float)$rider['seven_income'];
            $response['can_withdrawal_amount'] = (float)(isset($walletsInfo['can_withdrawal_amount'])?$walletsInfo['can_withdrawal_amount']:0);
        }
        $agent = [];
        if($identity == 3 || $identity == 4){
            $walletsInfo = $walletsModel->where(['user_id'=>$agent_id, 'type'=>'2'])->where('status', 'normal')->find();
            $agent = ProfitOrderService::calculateAgentIncome($agent_id);
            $response['total_income'] += (float)$agent['total_income'];
            $response['today_income'] += (float)$agent['today_income'];
            $response['yesterday_income'] += (float)$agent['yesterday_income'];
            $response['seven_income'] += (float)$agent['seven_income'];
            $response['can_withdrawal_amount'] += (float)(isset($walletsInfo['can_withdrawal_amount'])?(float)$walletsInfo['can_withdrawal_amount']:0);
        }
//
//        $response['rider'] = $rider;
//        $response['agent'] = $agent;
        return $response;

    }

    /**
     * desc:代理个人中心
     */
    public function center()
    {
        if(!$this->request->isPost()) {
            $this->error('请求类型错误');
        }
        $order_model = (new \app\api\model\xyb\Order());
        $receive_apply_model = (new \app\api\model\xyb\UserReceiveApply());

        $users = $this->getAuthUsers();
        $agent_info = (new AdminBelong())->where('bind_agent_user_id',$users->id)->find();

        $agent_id = (int)$agent_info->admin_id;

        $response = [
            'order' => ['pending' => 0, 'after_sales' => 0, 'today' => 0, 'total' => 0],
            'staff' => ['total' => 0, 'pending' => 0, 'audited' => 0],
            'agent' => [
                'agent_phone'   => $agent_info->phone,
                'agent_area'    => $agent_info->agent_area,
            ]
        ];

        $response['order']['pending'] = $order_model->where(['agent_id' => $agent_id, 'status' => 1])->count();

        $response['order']['after_sales'] = 0;

        $response['order']['today'] = $order_model->where(['agent_id' => $agent_id])->whereTime('createtime', 'today')->count();

        $response['order']['total'] = $order_model->where(['agent_id' => $agent_id])->count();

        $response['staff']['total'] = $receive_apply_model->where(['agent_id'=>$agent_id])->count();
        $response['staff']['pending'] = $receive_apply_model->where(['agent_id'=>$agent_id, 'status'=>0])->count();
        $response['staff']['audited'] = $receive_apply_model->where(['agent_id'=>$agent_id, 'status'=>1])->count();

        $this->success('请求成功', $response);
    }

    /**
     * desc:订单列表
     */
    public function order()
    {
        if(!$this->request->isPost()) {
            $this->error('请求类型错误');
        }
        $users = $this->getAuthUsers();
        $agent_info = (new AdminBelong())->where('bind_agent_user_id', $users->id)->find();
        $agent_id = (int)$agent_info->admin_id;

        $order_model = (new \app\api\model\xyb\Order());
        $where = [ 'agent_id'=>$agent_id];
        $param = $this->request->param();
        if(isset($param['status']) && $param['status']){
            $map['status'] = intval($param['status']);
        } else {
            $map['status'] = array('in',[1, 2, 3, 7, 8]);
        }
        $count = $order_model->where($where)->where($map)->count();
        $data = $order_model->where($where)
            ->where($map)
            ->alias('order')
            ->order('id desc')
            ->page($param['page'], $param['limit'])
            ->with(['user'])
            ->field("order.id,order.information,start,destination,hope_arrive,receive_id,
            type,total_fee,createtime,type,user_id,status,agent_id,mobile")//total_fee as price订单金额
            ->select();

        foreach ($data as $k => $v) {
            $push_users = UsersService::getUsersFoundation($v['user_id']);
            $data[$k]['nickname'] = $push_users['nickname'];
            $data[$k]['avatar'] = $push_users['avatar'];
            $data[$k]['dateline_create_date'] = XypHelper::xyb_dateline($v['createtime']);//创建时间
            $data[$k]['receive_users'] = UsersService::getReceivingUsersFoundation($v['receive_id'], $v['agent_id']);
            $data[$k]['income'] = OrdersService::CalculateOrdersRevenueProportion($v['total_fee'], $v['agent_id'], $v['type']);
            /*处理状态信息*/
            $data[$k]['status_text'] = OrdersService::formatOrdersStatus($v['status']);
        }
        $this->success('请求成功',['count'=>$count,'data'=>$data]);
    }

    /**
     * desc:申请列表
     */
    public function apply()
    {
        if(!$this->request->isPost()) {
            $this->error('请求类型错误');
        }
        $users = $this->getAuthUsers();
        $agent_info = (new AdminBelong())->where('bind_agent_user_id', $users->id)->find();
        $agent_id = (int)$agent_info->admin_id;
        $param = $this->request->param();
        if(isset($param['status']) && $param['status']){
            $status_map = [1=>0, 2=>-1];
            $map['status'] =$status_map[$param['stauts']];
        } else {
            $map['status'] = array('in',[0, -1]);
        }
        $receive_model = (new UserReceiveApply());
        $total = $receive_model->where(['agent_id'=>$agent_id])->count();
        $data = $receive_model->where(['agent_id'=>$agent_id])->order('id desc')
            ->page($param['page'], $param['limit'])
            ->field('user_id')
            ->select();
        $this->success('请求成功',['total'=>$total, 'data'=>$data]);
    }

    /**
     * desc:审核骑手
     */
    public function examine(){
        if(!$this->request->isPost()) {
            $this->error('请求类型错误');
        }
        if(!$user_id = ((int)$this->request->param('user_id'))){
            $this->error('参数错误');
        }
        if(!$status = ((int)$this->request->param('status'))){
            $this->error('参数错误');
        }
        $reject_reason = $this->request->param('reject_reason','');
        if(!in_array($status, [1, 2])){
            $this->error('参数错误');
        }
        if($status == 2 && !$reject_reason){
            $this->error('请填写拒绝理由');
        }
        $receive_model = (new UserReceiveApply());
        $receive_apply_info = $receive_model->where(['user_id'=>$user_id])->find();
        if(((int)$receive_apply_info->status) != 0){
            $this->error('审核状态错误');
        }
        $receive_apply_info->status = ($status == 2 ? -1 : 1);
        if($status == 2){
            $receive_apply_info->reject_reason = $reject_reason;
        }
        $receive_apply_info->updatetime = time();
        $receive_apply_info->save();
    }

    /**
     * desc:骑手列表
     */
    public function rider()
    {
        if(!$this->request->isPost()) {
            $this->error('请求类型错误');
        }
        $users = $this->getAuthUsers();
        $agent_info = (new AdminBelong())->where('bind_agent_user_id', $users->id)->find();
        $agent_id = (int)$agent_info->admin_id;
        $param = $this->request->param();
        $receive_model = (new UserReceiveApply());
        $total = $receive_model->where(['agent_id'=>$agent_id, 'status'=>1])->count();
        $data = $receive_model->where(['agent_id'=>$agent_id, 'status'=>1])->order('id desc')
            ->page($param['page'], $param['limit'])
            ->field('user_id')
            ->select();
        $this->success('请求成功',['total'=>$total, 'data'=>$data]);
    }

















































    public function indexval()
    {
        if(!$this->request->isPost()) {
            $this->error('请求类型错误');
        }

        $user_id_array = (new AuthGroupAccess())->where([
            'group_id'=>(new AuthGroup())->where('name','like','%代理%')->value('id'),
        ])->column('uid');
        $this->_search_filter($where);
        $where['admin_id'] = array('in',$user_id_array);
        $data = $this->model->getList((new AdminBelong()),$where,'admin_id id,agent_area');
        $this->success('请求成功',$data);
    }

    /**
     * desc:代理个人中心
     */
    public function center_bak()
    {
        if(!$this->request->isPost()) {
            $this->error('请求类型错误');
        }

        $users = $this->getAuthUsers();
        $param = $this->request->param();
        $bind_agent_id = (new AdminBelong())->where('bind_agent_user_id',$users->id)->value('admin_id');
        $user_id = $bind_agent_id;
        $walletsModel = new Wallets();
        $walletsInfo = $walletsModel->where(['user_id'=>$this->auth->id, 'type'=>'2', 'app_plat'=>$param['app_plat']])->find();
        $profitModel = new Profit();
        $orderReceiveModel = new OrderReceive();
        $where['agent_id'] = array('eq',$user_id);
        $where['app_plat'] = array('eq','MP-WEIXIN');
        $userReceiveApplyModel = new UserReceiveApply();
        $receive_id_array = $userReceiveApplyModel->where($where)->where('status', '1')->column('user_id');
        $map['receive_id'] = array('in',$receive_id_array);
        if(!$walletsInfo) {
            $total_income = 0.00;
            $can_withdrawal_amount = 0.00;
        } else {
            $total_income = $walletsInfo['total_income'];
            $can_withdrawal_amount = $walletsInfo['can_withdrawal_amount'];
        }

        $data = [
            'total_income'=>$total_income,
            'today_income'=>$profitModel->where($where)->whereTime('createtime', 'today')->sum('agent_profit'),
            'can_withdrawal_amount'=>$can_withdrawal_amount,
            'receiver_count'=>(new UserReceive())->where(['agent_id'=>$param['agent_id']])->where('status', 'normal')->count(),
            'week_income'=>$profitModel->where($where)->whereTime('createtime', 'week')->sum('agent_profit'),
            'week_finish_order_count'=>$orderReceiveModel->where('app_plat',$param['app_plat'])->where('status', '1')->where($map)->whereTime('createtime', 'week')->count(),
            'week_receive_count'=>$userReceiveApplyModel->where($where)->where('status', '1')->whereTime('updatetime', 'week')->count(),
//
            'month_income'=>$profitModel->where($where)->whereTime('createtime', 'month')->sum('agent_profit'),
            'month_finish_order_count'=>$orderReceiveModel->where('app_plat',$param['app_plat'])->where('status', '1')->where($map)->whereTime('createtime', 'month')->count(),
            'month_receive_count'=>$userReceiveApplyModel->where($where)->where('status', '1')->whereTime('updatetime', 'month')->count(),

            'year_income'=>$profitModel->where($where)->whereTime('createtime', 'year')->sum('agent_profit'),
            'year_finish_order_count'=>$orderReceiveModel->where('app_plat',$param['app_plat'])->where('status', '1')->where($map)->whereTime('createtime', 'year')->count(),
            'year_receive_count'=>$userReceiveApplyModel->where($where)->where('status', '1')->whereTime('updatetime', 'year')->count(),
        ];
        $this->success('请求成功',$data);
    }

    /**
     * desc:收入金额列表
     */
    public function profit_list()
    {
        if(!$this->request->isPost()) {
            $this->error('请求类型错误');
        }

        $profitModel = new Profit();
        $where['agent_id'] = array('eq',(new AdminBelong())->where('bind_agent_user_id',$this->auth->id)->value('admin_id'));
        $where['app_plat'] = array('eq','MP-WEIXIN');
        $data = $profitModel->getList($profitModel,$where,'desc,createtime,agent_profit as receive_profit','id desc');
        $this->success('请求成功',$data);
    }

    /**
     * desc:接单员待提现金额
     */
    public function can_withdrawal_amount()
    {
        if(!$this->request->isPost()) {
            $this->error('请求类型错误');
        }

        $walletsObj = (new Wallets())->where([
            'type'=>2,
            'user_id'=>$this->auth->id,
        ])->find();
        if(!$walletsObj) {
            $walletsObj['can_withdrawal_amount'] = 0;
        }
        $service_charge =$this->configObj->service_charge;
        $this->success('请求成功',['amount'=>$walletsObj['can_withdrawal_amount'],'service_charge'=>$service_charge]);
    }

    /**
     * desc接单员列表:
     */
    public function reveive_list()
    {
        if(!$this->request->isPost()) {
            $this->error('请求类型错误');
        }

        $agent_id = (new AdminBelong())->where('bind_agent_user_id',$this->auth->id)->value('admin_id');

        $param = $this->request->param();
        $total = (new UserReceive())
            ->where([
                'agent_id'=>$agent_id,
                'app_plat'=>$param['app_plat'],
            ])->count();
        $data = (new UserReceive())
            ->where([
                'agent_id'=>$agent_id,
                'app_plat'=>$param['app_plat'],
            ])->order('id desc')
            ->page($param['page'],$param['limit'])
            ->field('user_id')
            ->select();

        $orderReceiveModel = new OrderReceive();
        $profitModel = new Profit();
        if(!empty($data)) {
            foreach ($data as $k=>$v) {
                $wechatInfo = (new UserPlat())->alias('plat')
                    ->where([
                        'plat.user_id'=>$v['user_id'],
                    ])
                    ->join('xyb_user_wechat wechat','plat.wechat_id = wechat.id')
                    ->field('wechat.avatar,wechat.nickname')->find();
                if($wechatInfo) {
                    $data[$k]['nickname'] = $wechatInfo['nickname'];
                    $data[$k]['avatar'] = XypHelper::xyb_full_url($wechatInfo->avatar);
                } else {
                    $openid = (new UserBelong())->where('user_id',$v['user_id'])->value('openid');
                    if(!$openid) {
                        $this->error('用户微信数据丢失');
                    }
                    $wechatObj = (new UserWechat())->where('openid',$openid)->find();
                    if($wechatObj) {
                        $data[$k]['nickname'] = $wechatObj['nickname'];
                        $data[$k]['avatar'] =XypHelper::xyb_full_url($wechatObj->avatar);
                    } else {
                        $this->error('用户微信数据丢失了');
                    }

                }

                $data[$k]['receive_count'] = $orderReceiveModel->where([
                    'receive_id'=>$v['user_id'],
                    'app_plat'=>$param['app_plat'],
                ])->count();
                $data[$k]['profit'] = $profitModel->where([
                    'receive_id'=>$v['user_id'],
                    'agent_id'=>$agent_id,
                ])->sum('agent_profit');
            }
        }

        $this->success('请求成功',[
            'total'=>$total,
            'data'=>$data,
        ]);
    }

    /**
     * desc:代理手机号
     */
    public function phone()
    {
        if(!$this->request->isPost()) {
            $this->error('请求类型错误');
        }

        $agent_id = $this->request->param('agent_id');
        if (!$agent_id){
            $this->success('未选择学校');
        }else{
            $phone = (new AdminBelong())->where('admin_id',$agent_id)->value('phone');
            $this->success('请求成功',['phone'=>$phone]);
        }

    }

    /**
     * Desc  代理提交订阅
     * Create on 2024/5/29 14:55
     * Create by @小趴菜
     */
    public function submit_subscribe()
    {
        $count = 0;
        $subscribeObj = Db::name('xyb_agent_subscribe')->where('user_id', $this->auth->id)->where('type', 1)->find();
        if ($subscribeObj) {
            $count = Db::name('xyb_agent_subscribe')->where('user_id', $this->auth->id)->where('type', 1)->setInc('count');
        } else {
            $data = ['user_id' => $this->auth->id, 'count' => 1, 'type' => 1];
            $count = Db::name('xyb_agent_subscribe')->insert($data);
        }
        if ($count) {
            $this->success('订阅成功');
        } else {
            $this->error('订阅失败');
        }
    }

    /**
     * Desc  代理订阅数量
     * Create on 2024/5/29 14:55
     * Create by @小趴菜
     */
    public function subscribe_count()
    {
        $subscribeObj = Db::name('xyb_agent_subscribe')->where('user_id', $this->auth->id)->where('type', 1)->find();
        if ($subscribeObj) {
            $this->success('查询成功', ['subscribe_count' => $subscribeObj['count']]);
        } else {
            $this->success('查询成功', ['subscribe_count' => 0]);
        }
    }

}